package postgres

import (
	"context"
	"gorm.io/gorm"
	"manager/errors"
	"manager/model"
	"manager/model/entity"
	"manager/utils"
)

var VehicleSkill = &vehicleSkill{}

type vehicleSkill struct{}

// Create 创建
func (a *vehicleSkill) Create(ctx context.Context, m *entity.VehicleSkill) (int64, error) {
	err := GetDB(ctx).Create(m).Error
	return m.Id, err
}

// Find 查找详情
func (a *vehicleSkill) Find(ctx context.Context, in *model.VehicleSkillInfoRequest) (*entity.VehicleSkill, error) {
	e := &entity.VehicleSkill{}

	q := GetDB(ctx).Model(&entity.VehicleSkill{})

	if in.Id > 0 {
		err := q.First(&e, in.Id).Error
		return e, err
	}

	count := 0

	if in.EbikeTypeId != nil {

		q = q.Where("ebike_type_id = ?", in.EbikeTypeId)

		count++
	}

	if in.EbikeTypeName != nil {

		q = q.Where("ebike_type_name like ?", in.EbikeTypeName)

		count++
	}

	if in.Kind != nil {

		q = q.Where("kind = ?", in.Kind)

		count++
	}

	if in.Title != nil {

		q = q.Where("title like ?", in.Title)

		count++
	}

	if in.Content != nil {

		q = q.Where("content like ?", in.Content)

		count++
	}

	if in.Country != nil {

		q = q.Where("country like ?", in.Country)

		count++
	}

	if in.Language != nil {

		q = q.Where("language like ?", in.Language)

		count++
	}

	if in.Url != nil {

		q = q.Where("url like ?", in.Url)

		count++
	}

	if in.HeadImg != nil {

		q = q.Where("head_img like ?", in.HeadImg)

		count++
	}

	if in.NickName != nil {

		q = q.Where("nick_name like ?", in.NickName)

		count++
	}

	if in.UpdatedAt != nil {

		q = q.Where("updated_at = ?", in.UpdatedAt)

		count++
	}

	if count == 0 {
		return e, errors.New("condition illegal")
	}

	err := q.First(&e).Error
	return e, err
}

// Update 更新
func (a *vehicleSkill) Update(ctx context.Context, id int64, dict map[string]interface{}) error {
	return GetDB(ctx).Model(&entity.VehicleSkill{}).Where("id = ?", id).Updates(dict).Error
}

// Delete 删除
func (a *vehicleSkill) Delete(ctx context.Context, id int64) error {
	return GetDB(ctx).Delete(&entity.VehicleSkill{}, id).Error
}

// List 列表查询
func (a *vehicleSkill) List(ctx context.Context, in *model.VehicleSkillListRequest) (int, []*entity.VehicleSkill, error) {
	var (
		q             = GetDB(ctx).Model(&entity.VehicleSkill{}).Select("id, ebike_type_id, ebike_type_name, kind, title, country, language, url, head_img, nick_name, knowledge_type_name, updated_at,created_at")
		err           error
		total         int64
		vehicleSkills []*entity.VehicleSkill
	)

	if in.EbikeTypeId != nil {

		q = q.Where("ebike_type_id = ?", in.EbikeTypeId)

	}

	if in.EbikeTypeName != nil {

		q = q.Where("ebike_type_name like ?", in.EbikeTypeName)

	}

	if in.Kind != nil && *in.Kind > 0 {

		q = q.Where("kind = ?", in.Kind)

	}

	if in.Title != nil {

		q = q.Where("title like ?", utils.SqlLike(in.Title))

	}

	if in.Content != nil {

		q = q.Where("content like ?", in.Content)

	}

	if in.Country != nil {

		q = q.Where("country like ?", in.Country)

	}

	if in.Language != nil {

		q = q.Where("language like ?", utils.SqlLike(in.Language))

	}

	if in.Url != nil {

		q = q.Where("url like ?", in.Url)

	}

	if in.HeadImg != nil {

		q = q.Where("head_img like ?", in.HeadImg)

	}

	if in.NickName != nil {

		q = q.Where("nick_name like ?", utils.SqlLike(in.NickName))

	}

	if in.KnowledgeTypeName != nil {

		q = q.Where("KnowledgeTypeName like ?", utils.SqlLike(in.KnowledgeTypeName))

	}

	if in.UpdatedAt != nil {

		q = q.Where("updated_at = ?", in.UpdatedAt)

	}

	if err = q.Count(&total).Error; err != nil {
		return 0, nil, err
	}
	if err = q.Order("created_at desc").Limit(in.Size).Offset((in.Index - 1) * in.Size).Find(&vehicleSkills).Error; err != nil {
		return 0, nil, err
	}
	return int(total), vehicleSkills, nil
}

// ExecTransaction db事务执行
func (a *vehicleSkill) ExecTransaction(ctx context.Context, callback func(ctx context.Context) error) error {
	return GetDB(ctx).Transaction(func(tx *gorm.DB) error {
		ctx = context.WithValue(ctx, DBCONTEXTKEY, tx)
		return callback(ctx)
	})
}
